package com.qijian.maindata.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.maindata.domain.ProcessOrderUsers;
import com.qijian.maindata.domain.excel.ProcessOrderUsersExcelVo;
import com.qijian.maindata.domain.query.ProcessOrderQuery;
import com.qijian.maindata.domain.query.ProcessOrderUsersQuery;
import com.qijian.maindata.domain.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 工序工单派单人员Mapper接口
 *
 * @author qijian
 * @date 2021-10-09
 */
public interface ProcessOrderUsersMapper extends BaseMapper<ProcessOrderUsers> {

    Page<ProcessOrderUsersVo> selectVo(Page<ProcessOrderUsers> page, @Param(Constants.WRAPPER) Wrapper<ProcessOrderUsersQuery> queryWrapper, @Param("dataScope") QueryWrapper<ProcessOrderUsersQuery> dataScope);

    Page<ProcessOrderUsersDelayVo> selectDelayVo(Page<ProcessOrderUsers> p, @Param("page") Long page, @Param("pageSize") Long pageSize, @Param(Constants.WRAPPER) Wrapper<ProcessOrderQuery> queryWrapper);

    Long selectDelayVoCount(@Param(Constants.WRAPPER) Wrapper<ProcessOrderQuery> queryWrapper);

    Page<ProcessOrderUsersDelayVo> selectCheckVo(Page<ProcessOrderUsers> page, @Param(Constants.WRAPPER) Wrapper<ProcessOrderQuery> queryWrapper);

    List<ProcessOrderUsersVo> exportVo(@Param(Constants.WRAPPER) Wrapper<ProcessOrderQuery> queryWrapper);

    List<ProcessOrderUsersVo> selectVo(@Param(Constants.WRAPPER) Wrapper<ProcessOrderUsersQuery> queryWrapper, @Param("dataScope") QueryWrapper<ProcessOrderUsersQuery> dataScope);

    List<?> sumByProfit(@Param(Constants.WRAPPER) Wrapper<Object> queryWrapper);

    @Select("select SUM(IF(`STATUS` = 1,1,0)) waitCount," +
            "SUM(IF(`STATUS` = 2 OR `STATUS` = 3,1,0)) receiveCount," +
            "SUM(IF(`STATUS` = 4,1,0)) successCount " +
            "from main_process_order_users " +
            "where user_id = #{userId} and is_del = 0 " +
            "AND EXISTS (SELECT 1 FROM main_process_order po WHERE po.work_id = main_process_order_users.work_id AND po.order_type=1 and po.is_del = false)")
    ProcessOrderUsersCountVo countBySelf(Long userId);

    void updateByPower(@Param("orderUsers") ProcessOrderUsers orderUsers, @Param(Constants.WRAPPER) QueryWrapper<ProcessOrderUsersQuery> queryWrapper);

    List<ProcessOrderUsersVo> selectWorkCostByProjectNo(@Param("projectNo") String projectNo);

    List<ProcessOrderUserAssigned> getAssigned4WorkOrder(@Param("startTime") String startTime, @Param("endTime") String endTime, @Param("nickName") String nickName);

    List<ProcessOrderUsersExcelVo> exportVoNew(@Param(Constants.WRAPPER) Wrapper<ProcessOrderQuery> queryWrapper);

    List<ProcessOrderUsersTimeVo> countUnCheckedOrder(@Param("userId") Long userId);

    List<ProcessOrderUsersExcelVo> export4CostUsers(@Param("ym") String ym, @Param("userId") Long userId, @Param("deptIds") List<Long> deptIds);

}
